package com.aote.controller;

//体检套餐

import com.alibaba.dubbo.config.annotation.Reference;
import com.aote.constant.MessageConstant;
import com.aote.constant.RedisConstant;
import com.aote.entity.PageResult;
import com.aote.entity.QueryPageBean;
import com.aote.entity.Result;
import com.aote.pojo.Setmeal;
import com.aote.service.SetmealService;
import com.aote.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService setmealService;
//    文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();  //原始文件名
//        获取原始文件名最后点位置的index
        int index = originalFilename.lastIndexOf(".");
//        从.截取
        String extention = originalFilename.substring(index);
//        拼接图片名
        String fileName = UUID.randomUUID().toString()+extention;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
//            将图片传到redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }
//  新增套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        try{
            setmealService.add(setmeal,checkgroupIds);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

//    分页查询
   @RequestMapping("/findPage")
   public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.pageQuery(queryPageBean);
}

//查询基本数据
@RequestMapping("/findById")
public Result findById(Integer id) {
    try {
//        查询成功
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
    } catch (Exception e) {
//        查询失败
        e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }
}

//查询关系
@RequestMapping("/findCheckItemIdsByCheckGroupId")
public Result findCheckItemIdsByCheckGroupId(Integer id) {
    try {
//        查询成功
        List<Integer> list = setmealService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, list);
    } catch (Exception e) {
//        查询失败
        e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
    }
}

    //更新
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds) {
        try {
//        查询成功
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
//        查询失败
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
